﻿"""测试单选框"""
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
import random


class Application(tk.Frame):  # Application是应用的含义，需要继承Frame的初始化
    """一个经典的GUI程序的类的写法"""

    def __init__(self, master=None):  # 要传递master
        super().__init__(master)
        self.master = master
        self.pack()  # 通过布局管理显示
        self.create_widget()  # 调用类函数进行初始化小部件

    def create_widget(self):  # Widget是小部件的含义
        self.canvas = tk.Canvas(self, width=300, height=200, bg='green')
        self.canvas.pack()
        # 画折线
        line = self.canvas.create_line(10, 10, 30, 20, 50, 50)
        # 画矩形
        rect = self.canvas.create_rectangle(50, 50, 100, 100)
        # 画椭圆
        oval = self.canvas.create_oval(50, 50, 100, 100)

        global photo
        photo = tk.PhotoImage(file='../job_5_28/img/测试图片_gif.gif')
        # self.canvas.create_image(150, 170, image=photo)

        tk.Button(self, text='画10个矩形', command=self.draw50Recg).pack(side='left')

    def draw50Recg(self):
        for i in range(0, 10):
            x1 = random.randrange(int(self.canvas['width'])//2)
            y1 = random.randrange(int(self.canvas['height'])//2)
            x2 = x1 + random.randrange(int(self.canvas['width'])//2)
            y2 = y1 + random.randrange(int(self.canvas['height'])//2)
            self.canvas.create_rectangle(x1, y1, x2, y2)


def main():
    root = tk.Tk()
    root.geometry("800x500+200+300")
    root.title("测试label标签")
    app = Application(master=root)
    root.mainloop()


if __name__ == '__main__':
    main()
